﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using dokuzeylulworkshop.MasterUtilities;
using DokuzEylul.Sinav.Entities;
using System.Collections;
using System.Collections.Generic;
using Unipa.Framework.Data;


namespace DokuzEylul.Sinav.Utility_Master
{
    public class UserUtilities
    {
        private static UserUtilities sm_UserUtilities;
        private static HybridDatabase sm_database;
        public UserUtilities()
        {
            sm_database = dbmaster.Instance.dbfactory;
        }
        public static UserUtilities Instance
        {
            get
            {
                if (sm_UserUtilities == null)
                    sm_UserUtilities = new UserUtilities();
                return sm_UserUtilities;
            }
        }
        /// <summary>
        /// Kullaniciyi Database de doğrular
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public SinavUser authenticateUser(string username,string password)
        {
            SinavUser u = sm_database.SelectSingle<SinavUser>("Select * from SinavUser where UserName=? and Password=?", username, password);
            if (u != null)
            {
                return u;
            }
            else
                return null;
        }
        /// <summary>
       /// Kullanıcının Tüm rollerini geriye döner
       /// </summary>
       /// <param name="Current_User"></param>
       /// <returns></returns>
        public IList<SinavUserRole> getRoles(SinavUser Current_User)
        {
           
            IList<SinavUserRole> _result = sm_database.Select<SinavUserRole>("Select * from SinavUserRole where UserID=?", Current_User.UserId);
            if (_result.Count != 0)
                return _result;
            else
                throw new Exception("Bu Kullanıcı için Herhangi Bir Yetki Bulunamadı");
        }
        /// <summary>
        /// Kullanıcıların Uygulama yetkilerini getirir
        /// </summary>
        /// <param name="sinavUser">ilgili kullanıcı sınıfı</param>
        /// <returns></returns>
        public IList<SinavUserRole> getUygulamaRoles(SinavUser sinavUser)
        {
            string sql = "select sr.* " +
                         "from SinavMenu sm " +
                         "inner join SinavUserRole sr " +
                         "on sm.MenuId = sr.MenuId " +
                         "where sr.UserID=? and sm.MenuType=1 ";
            IList<SinavUserRole> _result = sm_database.Select<SinavUserRole>(sql, sinavUser.UserId);
            if (_result != null)
            {
                return _result;
            }
            throw new Exception("Bu kullanıcıya ait hiç bir yetki bulunamadı");

        }

        #region KullaniciMetodları

        /// <summary>
        /// Tüm yönetim panelindeki tüm kullanıcıların listesini getirir
        /// </summary>
        /// <returns></returns>
        public IList<SinavUser> GetSinavUsers()
        {
            return sm_database.Select<SinavUser>("select * from SinavUser");
        }
        public void SaveUser(SinavUser _user)
        {
            sm_database.Save(_user);
        }
        public void UpdateUser(SinavUser _user)
        {
            sm_database.Update(_user);
        }
        public void DeleteUser(SinavUser _user)
        {
            sm_database.DeleteDbEntity(_user);
        }
        public SinavUser GetUser(Guid _userId)
        {
            return sm_database.SelectSingle<SinavUser>("select * from SinavUser where UserId=?", _userId);
        }

        #endregion
        #region RolMetodları

        public IList<Role> GetAllRoles()
        {
            return sm_database.Select<Role>("select * from Role");
        }
        public Role GetRole(Guid _role)
        {
            return sm_database.SelectSingle<Role>("Select * from Role where RoleID = ?", _role);
        }
        public void SaveRole(Role _role)
        {
            sm_database.Save(_role);
        }
        public void UpdateRole(Role _role)
        {
            sm_database.Update(_role);
        }
        public void DeleteRole(Role _role)
        {
            sm_database.DeleteDbEntity(_role);
        }

        #endregion
    }
}
